package com.zhangh.summer.admin.support.spring;

import com.zhangh.summer.admin.support.boot.common.ApplicationConfig;
import io.micrometer.core.instrument.MeterRegistry;
import io.prometheus.client.exporter.MetricsServlet;
import io.prometheus.client.hotspot.DefaultExports;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @description: TODO
 * @author: zhanghui  zhanghlucky@163.com
 * @create: 2020-07-01 11:10
 **/

@Configuration
public class PrometheusCustomizerConfig {

    @Bean
    public MeterRegistryCustomizer<MeterRegistry> meterRegistryCustomizer() {
        String applicationName= ApplicationConfig.config().getApplicationName();
        return (registry) -> registry.config().commonTags("application", applicationName);
    }
    @Bean
    ServletRegistrationBean servletRegistrationBean() {
        DefaultExports.initialize();
        return new ServletRegistrationBean(new MetricsServlet(), new String[]{"/metrics"});
    }

}
